package com.yeebo.controller.sys;

import com.yeebo.common.BaseController;
import com.yeebo.conf.Log;
import com.yeebo.enums.ResultCode;
import com.yeebo.exception.ServiceException;
import com.yeebo.interceptor.Access;
import com.yeebo.rpc.admin.SysResourceService;
import com.yeebo.rpc.admin.SysUserService;
import com.yeebo.utils.IPUtils;
import com.yeebo.utils.ResponseUtils;
import com.yeebo.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class SysUserController extends BaseController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysResourceService sysResourceService;

    @Log(description = "登录接口")
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestParam("loginName") String loginName,
                        @RequestParam("password") String password,
                        HttpServletRequest request) {
        String ipAddress = IPUtils.getRemoteIP(request);
        return ResponseUtils.success(sysUserService.login(loginName, password, ipAddress));
    }

    @Access
    @RequestMapping("/getMyMenuList")
    @ResponseBody
    public Result getMyMenuList(HttpServletRequest request) {
        Long userId = this.getUserId(request);
        return ResponseUtils.success(sysResourceService.getMenuListByUserId(userId));
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Result logout() {
        sysUserService.logout();
        return ResponseUtils.success("退出成功");
    }

    @Access(needToken = true)
    @RequestMapping("/userInfo")
    @ResponseBody
    public Result userInfo(HttpServletRequest request) {
        Long userId = this.getUserId(request);
        return ResponseUtils.success(sysUserService.getUserById(userId));
    }

    @RequestMapping("/needLogin")
    @ResponseBody
    public Result needLogin() {
        throw new ServiceException(ResultCode.NEED_LOGIN);
    }

    @RequestMapping("/unAuthor")
    @ResponseBody
    public Result unAuthor() {
        throw new ServiceException(ResultCode.PERMISSION_DENY);
    }

}
